package com.tiny.work.biz.spring;

import com.tiny.work.biz.entity.Student;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

public class TinyWorkBeanDefinitionParser implements BeanDefinitionParser {

  @Override
  public BeanDefinition parse(Element element, ParserContext parserContext) {
    BeanDefinitionRegistry registry = parserContext.getRegistry();
    GenericBeanDefinition studentBean = new GenericBeanDefinition();
    studentBean.setBeanClass(Student.class);
    studentBean.getPropertyValues().addPropertyValue("name", element.getAttribute("name"));
    studentBean.getPropertyValues().addPropertyValue("age", element.getAttribute("age"));
    studentBean.getPropertyValues().addPropertyValue("id", element.getAttribute("id"));
    BeanDefinitionReaderUtils
        .registerBeanDefinition(new BeanDefinitionHolder(studentBean, "studentBean"), registry);
    return null;
  }
}
